/*
 * Created by JFormDesigner on Tue Jun 25 08:28:07 CST 2024
 */

package liyujie.ui;

import liyujie.dao.UserDao;
import liyujie.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author lenovo
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }

    private void btnLogin(ActionEvent e) {
//       获取文本框的输入的用户名和密码
        String name = textName.getText();
        String pass = txtPass.getText();
        System.out.println(name+","+pass);
//        name.matches("正则表达式") 返回true表示成功，返回false表示验证失败
        if(!name.matches("^\\w{3,10}$"))
        {
//            swing的消息对话框
            JOptionPane.showMessageDialog(this,"你输入的用户名不合法，请检查后重新输入");
            return;
        }
        if(!pass.matches("^\\w{3,10}$"))
        {
            JOptionPane.showMessageDialog(this,"你输入的密码不合法，请检查后输入");
            return;
        }
       UserDao userDao=new UserDao();
        User user=userDao.login(name,pass);
        if(user==null){
            JOptionPane.showMessageDialog(this,"错误");
        }else {
            JOptionPane.showMessageDialog(this,"成功");
            FormMain formMain=new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
            formMain.setVisible(true);
            formMain.setLocationRelativeTo(null);
            formMain.setVisible(true);
            this.dispose();
            
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - LILI
        txt = new JLabel();
        txt1 = new JLabel();
        txt2 = new JLabel();
        btnLogin = new JButton();
        btnReg = new JButton();
        textName = new JTextField();
        txtPass = new JPasswordField();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- txt ----
        txt.setText("\u7528\u6237\u767b\u5f55");
        contentPane.add(txt);
        txt.setBounds(155, 20, 80, 35);

        //---- txt1 ----
        txt1.setText("\u7528\u6237\u540d");
        contentPane.add(txt1);
        txt1.setBounds(new Rectangle(new Point(60, 70), txt1.getPreferredSize()));

        //---- txt2 ----
        txt2.setText("\u5bc6\u7801");
        contentPane.add(txt2);
        txt2.setBounds(60, 115, 40, 17);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.addActionListener(e -> btnLogin(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(110, 175), btnLogin.getPreferredSize()));

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        contentPane.add(btnReg);
        btnReg.setBounds(new Rectangle(new Point(210, 175), btnReg.getPreferredSize()));
        contentPane.add(textName);
        textName.setBounds(135, 70, 135, textName.getPreferredSize().height);
        contentPane.add(txtPass);
        txtPass.setBounds(135, 115, 135, txtPass.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - LILI
    private JLabel txt;
    private JLabel txt1;
    private JLabel txt2;
    private JButton btnLogin;
    private JButton btnReg;
    private JTextField textName;
    private JPasswordField txtPass;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
